/**
 * @file hb_spi.h
 * @synopsis  spi操作的头文件
 * @author wen wjcapple@163.com
 * @version 2.0
 * @date 2016-06-24
 */
#ifndef _HB_APP_H
#define _HB_APP_H_

#include <pthread.h>
#include "global.h"
#include "otdr_ch.h"
#include "glink.h"
#include "fpga-tr700.h"
#ifdef __cplusplus
extern "C" {
#endif

/*
 *下面定义的结构体在读取保存光纤段配置的时候使用，作为文件头信息
*/

//记录光纤段可变信息，此数据保存在文件头部
struct _tagFiberSecHead
{
	int32_t data_num;
	int32_t sec_num;
	int32_t event_num;
};

/*
 *下面定义的结构体是在找告警位置的时候使用
*/

//描述光纤段起始位置
struct _tagFiberSecCoord
{
	int32_t start;
	int32_t end;
};
//通过事件找到的告警点
struct _tagEventAlarmData
{
	int32_t index;
	int32_t pos;
	int32_t lev;
	int32_t sec;
	float	diff;
};
struct _tagEventAlarm
{
	struct _tagEventAlarmData first;
	struct _tagEventAlarmData highest;
};

/*********************************************************/

//初始化光纤段，节点名称等相关参数
int32_t initialize_sys_para();
//创建文件夹
//协议80000003,对应内容的保存，节点名称和地址
int32_t save_node_name_address(const struct _tagDevMisc *pdev_misc);
int32_t read_node_name_address(struct _tagDevMisc *pdev_misc);
//协议0x80000004内容，光纤段内容对应和保存
int32_t save_fiber_sec_para(int ch,
	       	struct tms_fibersectioncfg *pfiber_sec,
	       	struct _tagCHFiberSec *pch_fiber_sec,
		struct _tagOtdrDev *potdr_dev
	       	);
int32_t read_fiber_sec_para(int ch, struct _tagCHFiberSec *pch_fiber_sec);
//保存或者读取otdr fpga相关信息
int32_t save_ch_fpga_info(const struct _tagCHInfo *pch_fpga_info,int32_t ch_num);
int32_t read_ch_fpga_info(struct _tagCHInfo *pch_fpga_info,int32_t ch_num);
//创建文件夹
int32_t creat_folder(const char folder_path[]);
//为光纤段分配缓冲
int32_t alloc_fiber_sec_buf(struct _tagFiberSecHead secHead,
		struct _tagCHFiberSec *pch_fiber_sec);
//释放光纤段存储区
int32_t free_fiber_sec_buf(struct _tagCHFiberSec *pch_fiber_sec);
//初始化otdr模块
int32_t initialize_otdr_dev(struct _tagOtdrDev *pOtdrDev,
		int32_t ch_num);
//初始化光纤段参数
int32_t initialize_fiber_sec_cfg();
//初始化系统IP
int32_t initial_system_ip();
//更新通道参数
int32_t get_ch_para_from_fiber_sec(struct _tagCHPara *pCHPara, 
		const struct _tagCHFiberSec *FiberSec);
//自定义快速锁操作函数
int32_t quick_lock_init(QUICK_LOCK *plock);
int32_t quick_lock( QUICK_LOCK *plock);
int32_t quick_unlock( QUICK_LOCK *plock);
//检查用户点名测量参数
int32_t check_usr_otdr_test_para(struct tms_get_otdrdata *pget_otdrdata);
//检查用户配置光纤段测量参数
int32_t check_fiber_sec_para(const struct tms_fibersectioncfg *pfiber_sec_cfg);
//创建调度，算法任务
int32_t create_usr_tsk();
//通过目的地址获取context
int32_t get_context_by_dst(int32_t dst, struct tms_context **pcontext);
//回应
int32_t hb_Ret_Ack(int32_t dst, struct tms_ack ack);
int32_t hb_Ret_Ack_V2(struct glink_addr *addr, struct tms_ack *ack);
//读取槽位号
int32_t read_slot();
//读取网络标志号
int32_t read_net_flag();
#if 1 
//向主机发送曲线数据
//算法测量参数转换到hebei2协议中来
//自我了断
int32_t exit_self(int32_t err_code, const char *function,int32_t line, char msg[]);
//根据输的命令码返回对应曲线的CMD
int32_t get_ret_curv_cmd(int32_t in_cmd, int32_t *ret_cmd);
//获取告警级别
int32_t get_alarm_lev(
		float loss,
		struct tms_fibersection_val *pfiber_sec
		);
//获取插损的差值
int32_t get_inser_loss_diff(
		float cur_loss,
		float std_loss,
		float *diff_loss
		);
//返回节点基本信息
int32_t ret_host_basic_info(
		struct tms_context *pcontext,
		struct glink_addr *paddr);

//发送总的告警
int32_t ret_total_curalarm2host();
int32_t ret_total_curalarm2host_V2(struct glink_addr *paddr);
//获取该通道内所有的告警
int32_t get_ch_total_alarm(
		struct tms_alarmlist_val alarm_val[], 
		struct _tagUpOtdrCurv *pcurv,
		struct _tagCHFiberSec *pfiber_sec,
		struct _tagOtdrDev *potdr_dev
		);

//将周期性测量曲线转换成host格式
int32_t convert_ch_cyc_curv2host(
		int32_t ch,
		struct tms_alarmline_val line_val[], 
		struct _tagUpOtdrCurv *pcyc_curv
		);
#endif
//保存采样点，调试时使用
int32_t save_data_pt(const char file[], float *An, int32_t len);
//修改设备ip，在网段发生改变的时候使用
int32_t modifiy_eq_ip(const char ip[], const char mask[], const char gw[]);
//返回周期性测量曲线
int32_t ret_host_cyc_curv(
		const struct tms_context *pcontext, 
		struct _tagCycCurv *pcyc_curv,
		int32_t ch);
//返回标准曲线
int32_t ret_host_std_cfg(
		const struct tms_context *pcontext, 
		struct _tagCHFiberSec *pchfiber_sec,
		int32_t ch);
//返回统计数据
int32_t ret_host_statis_data(
		int32_t ch, 
		struct tms_context *pcontext,
		struct _tagCHFiberSec *pchfiber_sec);
//检查本机IP
int32_t check_local_ip(int32_t flag, int32_t slot);
//获取pkid
int16_t get_pkid();
int32_t ret_host_otdr_curv(
		const OtdrTestResult_t *tr, 
		const struct _tagUsrOtdrTest *pusrTest);
//初始化波长
int32_t initial_lamda(struct _tagOtdrDev *potdr, struct fpga_info *pfpga);
int32_t initial_algor_moudle();
//初始化otdr测试控制变量
int32_t initial_otdr_ctrl(struct _tagOtdrTestCtrl *pTestCtrl, struct fpga_info *fpga);
//更新被监测通道数目
int32_t refresh_monior_ch_num();
//更新通道使能状态
int32_t refresh_ch_state(int32_t state);
//修正光线段中的通道号
int32_t adjust_fiber_sec_cfg_ch();
//获取本地IP
int32_t get_local_ip(char eth[],char (*ip)[16]);
int32_t set_ch_alarm_status(int32_t ch, int32_t val, struct _tagCHAlarmDis *pch_dis);
//系统启动的时候复位全部的led灯
int32_t reset_all_alarm_led(struct _tagGpioFd *pgpio_fd);
int32_t run_led_blink();
int32_t get_gpio_fd(const char *env_name,int32_t mod);
int32_t light_all_led(int32_t val);
int32_t clr_fiber_sec_para(int32_t ch,
		struct _tagCHFiberSec *pch_fiber_sec,
		struct _tagOtdrDev *potdr_dev);
int32_t RetLocalCardType(struct tms_context *pcontext);

int get_sys_time(char time_buf[], int offset, const char format[]);
int32_t anylyze_fpga_Info(struct fpga_info *fpga);
//根据通道号分配内存
int32_t alloc_eq_mem(int32_t ch_num);
//根据通道数目设定设备的源地址，设备类型
int32_t initial_device_type(int32_t num_ch);
int32_t initial_ch_fiber_sec(int ch,
		struct tms_fibersectioncfg *pfiber_sec,
		struct _tagCHFiberSec *pch_fiber_sec,
		struct _tagOtdrDev *potdr_dev	);
#ifdef __cplusplus
	}
#endif

#endif

